package com.subsequent.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.subsequent.mapper.DepartmentsMapper;
import com.subsequent.pojo.Departments;
import com.subsequent.service.DepartmentsService;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;

@Service
public class DepartmentsServiceImpl extends ServiceImpl<DepartmentsMapper, Departments> implements DepartmentsService {

    @Override
    public Page<Departments> pageQuery(int pageNum, int pageSize, Integer campusId, Integer categoryId, String deptName, Integer status, Date startTime, Date endTime) {
        LambdaQueryWrapper<Departments> wrapper = new LambdaQueryWrapper<>();
        if (campusId != null) {
            wrapper.eq(Departments::getCampusId, campusId);
        }
        if (categoryId != null) {
            wrapper.eq(Departments::getCategoryId, categoryId);
        }
        if (deptName != null && !deptName.isEmpty()) {
            wrapper.like(Departments::getDeptName, deptName);
        }
        if (status != null) {
            wrapper.eq(Departments::getStatus, status);
        }
        if (startTime != null) {
            wrapper.ge(Departments::getCreatedAt, startTime);
        }
        if (endTime != null) {
            wrapper.le(Departments::getCreatedAt, endTime);
        }
        wrapper.orderByAsc(Departments::getDeptId).orderByDesc(Departments::getCreatedAt);
        Page<Departments> page = new Page<>(pageNum, pageSize);
        return this.page(page, wrapper);
    }

    @Override
    public List<Departments> tree(Integer campusId, Integer categoryId, Integer status, Date startTime, Date endTime) {
        LambdaQueryWrapper<Departments> wrapper = new LambdaQueryWrapper<>();
        if (status != null) {
            wrapper.eq(Departments::getStatus, status);
        }
        if (startTime != null) {
            wrapper.ge(Departments::getCreatedAt, startTime);
        }
        if (endTime != null) {
            wrapper.le(Departments::getCreatedAt, endTime);
        }
        wrapper.orderByAsc(Departments::getDeptId).orderByDesc(Departments::getCreatedAt);
        return this.list(wrapper);
    }

}